<template>
    <div class="e-form">
        <BillTop title="租赁计划新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.planLease" label-width="160px" :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="计划日期：" prop="billDate">
                            <el-date-picker
                                v-model="formData.planLease.billDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="配置计划编号：" prop="configurationPlanNo">
                            <el-input v-model="formData.planLease.configurationPlanNo" :disabled="isDisabled" readonly="true">
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectPlanNo"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="填报人：">
                            <span>{{ formData.planLease.recorder }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="费用合计（元）：">
                            <span>{{ formData.planLease.totalCost }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="变更期数：">
                            <span>{{ formData.planLease.number }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="机构：">
                                <span>{{ userInfo.orgInfo.orgName }}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：">
                            <span>{{ statusLable(formData.planLease.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                       <el-form-item label="租赁类型：" prop="leaseType">
                            <el-select
                                v-model="formData.planLease.leaseType"
                                placeholder="请选择"
                                 @change="leaseTypeChange"
                            >
                                 <el-option
                                    v-for="(
                                        item, index
                                    ) in leaseTypeList"
                                    :key="index"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="币种：" prop="currencyId">
                            <el-select
                                v-model="formData.planLease.currencyId"
                                placeholder="请选择币种"
                                @change="currencyChange"
                            >
                                 <el-option
                                    v-for="(
                                        item
                                    ) in currencyForm"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item  label="租赁说明：">
                        <el-input type="textarea" v-model="formData.planLease.remarks" placeholder="请输入内容"></el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="人民币汇率：">
                            <span>{{formData.planLease.rmbRate}}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12" v-if="formData.planLease.leaseType == '2'">
                        <el-form-item label="传输财务共享：">
                            <span :class="isFontColor ? 'redFont' : 'greenFont'">{{shareLable(formData.planLease.share)}}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose"  v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/equipPlan.js'
import requestAudit from '@/api/equip/common.js'
import { mapState } from 'vuex'
import { selectDataClient } from '@/utils/common'
export default {
    data () {
        return {
            formData: {
                //配置计划基础信息
                'planLease': {
                    'auditCost': 0,
                    'auditTime': '',
                    'auditor': '',
                    'auditorId': '',
                    'baseCurId': '',
                    'baseCurName': '',
                    'baseCurRate': 0,
                    'baseCurTotalCost': 0,
                    'billDate': '',
                    'billId': '',
                    'billNo': '',
                    'changeBillId': '',
                    'changeBillNo': '',
                    'changeState': null,
                    'changeTotalCost': 0,
                    'configurationPlanId': '',
                    'configurationPlanNo': '',
                    'currency': '',
                    'currencyId': '',
                    'esaId': '',
                    'founderId': '',
                    'founderName': '',
                    'isExcss': 0,
                    'isTemporaryLease': '',
                    'leaseType': '',
                    'modifier': '',
                    'modifierId': '',
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDescription': '',
                    'nullifyReason': '',
                    'number': 0,
                    'orgId': '',
                    'orgName': '',
                    'recorder': '',
                    'recorderId': '',
                    'remarks': '',
                    'rmbId': '',
                    'rmbName': '',
                    'rmbRate': 0,
                    'rmbSumTotalCost': 0,
                    'settleAmount': 0,
                    'share': 0,
                    'source': '',
                    'state': 0,
                    'totalCost': 0,
                    'version': 0,
                    'workId': ''
                }
            },
            rules: {
                billDate: [
                    { required: true, message: '请选择日期', trigger: 'blur' },
                ],
                currencyId: [
                    { required: true, message: '请选择币种', trigger: ['blur', 'change']  },
                ],
                leaseType: [
                    { required: true, message: '请选择租赁类型', trigger: ['blur', 'change'] },
                ],
                // configurationPlanNo: [
                //     { required: false, validator: (rule, value, callback)=>{
                //         if(this.formData.planLease.leaseType !== '2' && !this.formData.planLease.configurationPlanNo) {
                //             callback(new Error('请选择配置计划编号'))

                //         }
                //         else {
                //             callback()
                //         }
                //     }, trigger: ['blur', 'change'] }
                // ]
            },
            isDisabled: false,
            test: '',
            isFontColor: true, //传输财务共享颜色  未传，返回修改 红色   财务共享审核中，已传 绿色

        }
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
            currencyForm: state => state.equip.equipData.currencyForm, //币种
            leaseTypeList: state => state.equip.equipData.leaseTypeList, //租赁类型
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return (obj ?  obj.label :  '')
            }
        },
        //传输财务共享
        shareLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.shareFrom
                const obj = state.find(x=>x.value === param)
                return (obj ?  obj.label :  '')
            }
        },
    },
    created () {
        this.getInfo()
    },
    mounted () {
    },
    methods: {
        getInfo () { //默认获取数据
            this.typeTest = false
            const date = new Date()
            this.formData.planLease.billDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
            const { baseCyByOrgId } = this.userInfo
            this.formData.planLease.baseCurName = baseCyByOrgId.currencyName  //本位币名称
            this.formData.planLease.baseCurId = baseCyByOrgId.currency //本位币id
            this.formData.planLease.recorder = localStorage.getItem('userName')
            this.shareFontColor()

        },
        //传输财务共享字体颜色  未传，返回修改 红色   财务共享审核中，已传 绿色
        shareFontColor () {
            if(this.formData.planLease.share === 0 || this.formData.planLease.share === 1) {
                this.isFontColor = true
            }else{
                this.isFontColor = false
            }
        },
        // 弹窗选择配置计划编号
        selectPlanNo () {
            if(this.isDisabled) {
                return
            }
            let ids = []
            // ids = this.formData.planConfigurationDtlList.map(x=>x.accountId)
            selectDataClient({
                SelectServiceToken: '/facilityplan/get/configuration/plan', // 客户端对应方法名
                IsSingleMode: true, // 是否单选 true 单选 false 多选
                SelectedIDs: ids, // 已选中id
                Title: '选择配置计划编号' // 标题
            }).then(val => {
                this.formData.planLease.configurationPlanNo = val[0].billNo
                this.formData.planLease.configurationPlanId = val[0].billId
            })
        },
        //币种切换
        currencyChange (val) {
            this.formData.planLease.currency = this.currencyForm.find(x=>x.value === val).label  //币种名称
            requestAudit.getRmbRate({  //获取机构下某币种的人民币汇率
                currency: val,
                orgId: this.userInfo.orgInfo.orgId
            }).then(res=>{
                if(res.rate === 1) {
                    this.formData.planLease.rmbRate = res.rate
                    this.formData.planLease.rmbId = res.currency
                }else{
                    this.formData.planLease.rmbRate = res.rate
                    this.formData.planLease.rmbId = res.rateId
                }

            }).catch(()=>{

            })
        },
        //租赁类型切换
        leaseTypeChange (val) {
            this.isDisabled = val === '2' ? true : false
            if(this.isDisabled) {
                this.formData.planLease.configurationPlanNo = ''
                this.formData.planLease.configurationPlanId = ''
            }
            // if(this.typeTest) {
            //     this.clientPop(
            //         'info',
            //         '切换租赁类型，配置计划编号将被清空，是否继续？', ()=>{
            //             this.isDisabled = val === '2' ? true : false
            //             this.formData.planLease.configurationPlanNo = ''
            //             this.formData.planLease.configurationPlanId = ''
            //         })
            // }else{
            //     this.typeTest = true
            // }
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if (valid) {
                    request.leaseListAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/houseLeasePlanEdit',
                                query: {
                                    billid: res,
                                    name: 'changeDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                } else {
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/houseLeasePlanList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
